﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Dictionary<string, object> otherInfos = new Dictionary<string, object>();
            otherInfos["Persons"]= new List<Person>{
                    new Person(){Name="Francesco", Surname="Abbruzzese"}, 
                    new Person{Name="John", Surname="Smith"}, 
                    new Person(){Name="Jack", Surname="Petrone"}};
            var wrapper = new BindToolkit.BindWrapper(new Interval() { 
                InnerInterval = new Interval(), 
                OtherInfos=otherInfos,
                Margin = 5
            }, 
            true, true);
            this.DataContext = wrapper;
        }
        bool isValid;
        private ValidationException FakeException(string field, string message)
        {
            return new ValidationException(
                new System.ComponentModel.DataAnnotations.ValidationResult(message, new string[] { field }),
                null, null);


        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (validationErrorNumbers == 0)
            {
                
                if ((this.DataContext as BindToolkit.BindWrapper).ValidateWholeObject(
                    ))
                {
                    isValid = true;
                    // do Actions Here!!!!!!!
                }
            }     
            
        }
        private int validationErrorNumbers = 0;
        private void Window_Error(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added) validationErrorNumbers++;
            else validationErrorNumbers--;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            (this.DataContext as BindToolkit.BindWrapper).AddRemoteErrors(
            new ValidationException[] { FakeException("InnerInterval.Low", "Fake Exception!"), FakeException("OtherInfos[Persons][1].Name", "Fake Exception") });
        }
    }
}
